package com.ruoyi.report_ljlm.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_ljlm.domain.GzsdbgBsDetail;
import com.ruoyi.report_ljlm.domain.vo.GzsdbgBsDetailVo;
import com.ruoyi.report_ljlm.mapper.GzsdbgBsDetailMapper;
import com.ruoyi.syrw.annotation.ComputeReportResult;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_ljlm.domain.bo.GzsdbgBsInfoBo;
import com.ruoyi.report_ljlm.domain.vo.GzsdbgBsInfoVo;
import com.ruoyi.report_ljlm.domain.GzsdbgBsInfo;
import com.ruoyi.report_ljlm.mapper.GzsdbgBsInfoMapper;
import com.ruoyi.report_ljlm.service.IGzsdbgBsInfoService;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 路面构造深度试验检测报告Service业务层处理
 *
 * @author llb
 * @date 2024-07-10
 */
@RequiredArgsConstructor
@Service
public class GzsdbgBsInfoServiceImpl implements IGzsdbgBsInfoService {

    private final GzsdbgBsInfoMapper baseMapper;
    private final GzsdbgBsDetailMapper detailMapper;

    /**
     * 查询路面构造深度试验检测报告
     */
    @Override
    @FIllBasicInfo
    public GzsdbgBsInfoVo queryById(Long excelId) {
        GzsdbgBsInfoVo gzsdbgBsInfoVo = baseMapper.selectVoById(excelId);
        List<GzsdbgBsDetailVo> detailList = detailMapper.selectVoList(
            new LambdaQueryWrapper<GzsdbgBsDetail>().eq(GzsdbgBsDetail::getExcelId, excelId));
        if (CollUtil.isNotEmpty(detailList)) {
            gzsdbgBsInfoVo.setDetailList(detailList);
        }
        return gzsdbgBsInfoVo;
    }

    /**
     * 查询路面构造深度试验检测报告列表
     */
    @Override
    public TableDataInfo<GzsdbgBsInfoVo> queryPageList(GzsdbgBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<GzsdbgBsInfo> lqw = buildQueryWrapper(bo);
        Page<GzsdbgBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询路面构造深度试验检测报告列表
     */
    @Override
    public List<GzsdbgBsInfoVo> queryList(GzsdbgBsInfoBo bo) {
        LambdaQueryWrapper<GzsdbgBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<GzsdbgBsInfo> buildQueryWrapper(GzsdbgBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<GzsdbgBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getTotalPage() != null, GzsdbgBsInfo::getTotalPage, bo.getTotalPage());
        lqw.eq(bo.getPage() != null, GzsdbgBsInfo::getPage, bo.getPage());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancdwmc()), GzsdbgBsInfo::getJiancdwmc, bo.getJiancdwmc());
        lqw.eq(StringUtils.isNotBlank(bo.getBaogbh()), GzsdbgBsInfo::getBaogbh, bo.getBaogbh());
        lqw.eq(StringUtils.isNotBlank(bo.getShigwtdw()), GzsdbgBsInfo::getShigwtdw, bo.getShigwtdw());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcmc()), GzsdbgBsInfo::getGongcmc, bo.getGongcmc());
        lqw.eq(StringUtils.isNotBlank(bo.getGongcbwyt()), GzsdbgBsInfo::getGongcbwyt, bo.getGongcbwyt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), GzsdbgBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), GzsdbgBsInfo::getYangpbh, bo.getYangpbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsl()), GzsdbgBsInfo::getYangpsl, bo.getYangpsl());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpzt()), GzsdbgBsInfo::getYangpzt, bo.getYangpzt());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpsj()), GzsdbgBsInfo::getYangpsj, bo.getYangpsj());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyj()), GzsdbgBsInfo::getJiancyj, bo.getJiancyj());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyj()), GzsdbgBsInfo::getPandyj, bo.getPandyj());
        lqw.eq(StringUtils.isNotBlank(bo.getZhuyyqsbmcjbh()), GzsdbgBsInfo::getZhuyyqsbmcjbh, bo.getZhuyyqsbmcjbh());
        lqw.eq(StringUtils.isNotBlank(bo.getWeitbh()), GzsdbgBsInfo::getWeitbh, bo.getWeitbh());
        lqw.eq(StringUtils.isNotBlank(bo.getJianclb()), GzsdbgBsInfo::getJianclb, bo.getJianclb());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancld()), GzsdbgBsInfo::getJiancld, bo.getJiancld());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegcc()), GzsdbgBsInfo::getJiegcc, bo.getJiegcc());
        lqw.eq(StringUtils.isNotBlank(bo.getHunhllx()), GzsdbgBsInfo::getHunhllx, bo.getHunhllx());
        lqw.eq(bo.getShejgzsd() != null, GzsdbgBsInfo::getShejgzsd, bo.getShejgzsd());
        lqw.eq(bo.getShejz1() != null, GzsdbgBsInfo::getShejz1, bo.getShejz1());
        lqw.eq(bo.getShejz2() != null, GzsdbgBsInfo::getShejz2, bo.getShejz2());
        lqw.eq(bo.getPingjz() != null, GzsdbgBsInfo::getPingjz, bo.getPingjz());
        lqw.eq(bo.getBiaozc() != null, GzsdbgBsInfo::getBiaozc, bo.getBiaozc());
        lqw.eq(bo.getBianyxs() != null, GzsdbgBsInfo::getBianyxs, bo.getBianyxs());
        lqw.eq(StringUtils.isNotBlank(bo.getJiegpd()), GzsdbgBsInfo::getJiegpd, bo.getJiegpd());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancjl()), GzsdbgBsInfo::getJiancjl, bo.getJiancjl());
        lqw.eq(StringUtils.isNotBlank(bo.getFujsm()), GzsdbgBsInfo::getFujsm, bo.getFujsm());
        lqw.eq(StringUtils.isNotBlank(bo.getBgFlag()), GzsdbgBsInfo::getBgFlag, bo.getBgFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getJianc()), GzsdbgBsInfo::getJianc, bo.getJianc());
        lqw.eq(StringUtils.isNotBlank(bo.getJil()), GzsdbgBsInfo::getJil, bo.getJil());
        lqw.eq(StringUtils.isNotBlank(bo.getFuh()), GzsdbgBsInfo::getFuh, bo.getFuh());
        lqw.eq(StringUtils.isNotBlank(bo.getRiq()), GzsdbgBsInfo::getRiq, bo.getRiq());
        lqw.eq(StringUtils.isNotBlank(bo.getShebIds()), GzsdbgBsInfo::getShebIds, bo.getShebIds());
        lqw.eq(StringUtils.isNotBlank(bo.getPandyjIds()), GzsdbgBsInfo::getPandyjIds, bo.getPandyjIds());
        lqw.eq(StringUtils.isNotBlank(bo.getJiancyjIds()), GzsdbgBsInfo::getJiancyjIds, bo.getJiancyjIds());
        return lqw;
    }

    /**
     * 新增路面构造深度试验检测报告
     */
    @Override
    @DSTransactional
    public Boolean insertByBo(GzsdbgBsInfoBo bo) {
        GzsdbgBsInfo add = BeanUtil.toBean(bo, GzsdbgBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
            ArrayList<GzsdbgBsDetail> details = new ArrayList<>(8);
            for (int i = 1; i <= 8; i++) {
                details.add(new GzsdbgBsDetail(bo.getExcelId()));
            }
            detailMapper.insertBatch(details);
        }
        return flag;
    }

    /**
     * 修改路面构造深度试验检测报告
     */
    @Override
    @DSTransactional
    @ComputeReportResult
    public Boolean updateByBo(GzsdbgBsInfoBo bo) {
        // 增加对 是否合格 的填充
        fillExcelResult(bo);
        GzsdbgBsInfo update = BeanUtil.toBean(bo, GzsdbgBsInfo.class);
        validEntityBeforeSave(update);
        boolean flag = baseMapper.updateById(update) > 0;
        if (flag) {
            detailMapper.updateBatchById(BeanUtil.copyToList(bo.getDetailList(), GzsdbgBsDetail.class));
        }
        return flag;
    }

    private void fillExcelResult(GzsdbgBsInfoBo bo) {
        if ("不符合".equals(bo.getJiegpd())) {
            bo.setExcelResult(2);
            return;
        }
        bo.setExcelResult(1);
    }

    public boolean isWell(Long excelId) {
        return baseMapper.selectVoById(excelId).getExcelResult() != 2;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(GzsdbgBsInfo entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除路面构造深度试验检测报告
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public GzsdbgBsInfoVo jcjl(GzsdbgBsInfoVo vo) {
        StringBuilder result = new StringBuilder("检测结论：经检测，所检指标均符合JTG F40-2004《公路沥青路面施工技术规范》，JTG F80/1-2017《公路工程质量检验评定标准》及设计文件要求。");
        vo.setJiancjl(result.toString());
        return vo;
    }
}
